
import os
import sys
import ConfigParser

import shutil

Config = ConfigParser.ConfigParser()
Config.read("config.ini")
THUMBSPATH = Config.get('SETTINGS','thumbspath')
FANARTPATH = Config.get('SETTINGS','fanartpath')


if os.path.isdir(THUMBSPATH):
    thumbs = THUMBSPATH
if os.path.isdir(FANARTPATH):
    fanart = FANARTPATH

#Given a full path remove the mount point.  This would be
#/mnt/DriveName on Unix or C:\ in Windows
def getRootFolder(path):
    oldpath = path
    path = os.path.abspath(path)
    while path != os.path.sep:
        if os.path.ismount(path):
            newpath =  path
            break
            #print newpath
        path = os.path.abspath(os.path.join(path, os.pardir))
        #newpath = oldpath.replace(path,"",1)
    return oldpath.replace(newpath,'',1)

#Returns Mount Point
def getMount(path):
    oldpath = path
    path = os.path.abspath(path)
    while path != os.path.sep:
        if os.path.ismount(path):
            newpath =  path
            return newpath
        path = os.path.abspath(os.path.join(path, os.pardir))
        #newpath = oldpath.replace(path,"",1)
    return newpath

#Adds the size of all files in path tree
def getDirectorySize(directory):
    dir_size = 0
    for (path, dirs, files) in os.walk(directory):
        for file in files:
            filename = os.path.join(path, file)
            dir_size += os.path.getsize(filename)
    return dir_size

#
def setHash(oldCRC,newCRC,oldFilename):
    hashchanges = 0
    thumbfolder = newCRC[0]
    oldfolder = thumbs +os.sep +oldCRC[0]
    movefolder = thumbs +os.sep + thumbfolder
    #copy old thumbs to new - Leaving old for now since
    #clean database may remove them - #TODO Check that
    #Line below for testing only - can remove along with
    # 3rd arg

    tempname = oldFilename
    if tempname.endswith(os.sep):
        tempname = tempname[0:-1]
    if not os.path.isdir(movefolder):
        os.mkdir(thumbs +os.sep + thumbfolder)
    if os.path.isfile(oldfolder+os.sep+oldCRC+".tbn"):
        # The original thumb was found so copy it to new name
        shutil.copy(oldfolder+os.sep+oldCRC+".tbn",movefolder+os.sep+newCRC+".tbn")
        if os.path.isfile(movefolder+os.sep+newCRC+".tbn"):
        #Since new files exists we can get rid of old file
            try:
                os.remove(oldfolder+os.sep+oldCRC+".tbn")
            except:            
                hashchanges = hashchanges         
        hashchanges = hashchanges+1
    #Season thumbs for TV = "season"+strpath
    
    # Do the same thing with the Fanart
    # folder is fanart. files can be in
    # main fanart or subdirectory
    fanartfolder = newCRC[0]
    oldfolder = fanart +os.sep +oldCRC[0]
    movefolder = fanart +os.sep + fanartfolder
    
    if not os.path.isdir(movefolder):
        os.mkdir(fanart +os.sep + fanartfolder)
    if os.path.isfile(movefolder+os.sep+oldCRC+".tbn"):
        shutil.copy(movefolder+os.sep+oldCRC+".tbn,",movefolder+os.sep+newCRC+".tbn")
    oldFanart = fanart+os.sep+oldCRC+".tbn"
    newFanart = fanart+os.sep+newCRC+".tbn"
    if os.path.isfile(oldFanart):
        # The original fanart was found so copy it to new name
        shutil.copy(oldFanart,newFanart)
        if os.path.isfile(newFanart):
            os.remove(oldFanart)  
            hashchanges = hashchanges +1
           #logging.debug(u"Thumbs and Fanart Changed")
    return hashchanges

def setNewCRC(newPath):
    newCRC = newPath
    return newCRC
